package com.camel.docker.controller;

import com.alibaba.fastjson.JSONObject;
import com.camel.docker.entity.Result;
import com.camel.docker.entity.docker.MyPullImageResultCallback;
import com.camel.docker.enums.ResultEnum;
import com.camel.docker.service.DockerClientService;
import com.camel.docker.utils.ResultUtil;
import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.command.InspectImageResponse;
import com.github.dockerjava.api.exception.NotFoundException;
import com.github.dockerjava.api.model.Image;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/images")
public class ImagesController extends BaseController {

    public static final String PRE_IMAGE_ID_NAME = "sha256:";

    @Autowired
    public DockerClientService dockerClientService;

    @Autowired
    public MyPullImageResultCallback myPullImageResultCallback;

    @GetMapping
    public Result images() {
        DockerClient dockerClient = dockerClientService.connect();
        PageInfo<Image> pageInfo = getPageInfo();
        List<Image> containerList = dockerClient.listImagesCmd().withShowAll(false).exec();
        pageInfo.setList(containerList);
        return ResultUtil.success(pageInfo);
    }

    @PostMapping
    public Result addImage(String image) {
        DockerClient dockerClient = dockerClientService.connect();

        if(StringUtils.isEmpty(image)) {
            return ResultUtil.error(ResultEnum.NOT_VALID_PARAM.getCode(), "无效的镜像");
        }
        List<String> list = CollectionUtils.arrayToList(image.split(":"));
        dockerClient.pullImageCmd(list.get(0)).withTag(list.get(1)).exec(myPullImageResultCallback);
        return ResultUtil.success("正在下载镜像");
    }

    @DeleteMapping("/{id}")
    public Result deleteImages(@PathVariable String id) {
        if(StringUtil.isNotEmpty(id)) {
            id = id.substring(7, id.length());
        }
        DockerClient dockerClient = dockerClientService.connect();
        dockerClient.removeImageCmd(id).withForce(true).exec();
        return ResultUtil.success("镜像删除成功");
    }

    @GetMapping("/{id}")
    public Result image(@PathVariable String id) {
        if(StringUtil.isNotEmpty(id) && id.length() > PRE_IMAGE_ID_NAME.length()) {
            if(id.startsWith(PRE_IMAGE_ID_NAME)) {
                id = id.substring(PRE_IMAGE_ID_NAME.length());
            }
        }else {
            return ResultUtil.error(ResultEnum.NOT_VALID_PARAM.getCode(), "找不到对应的镜像");
        }
        DockerClient dockerClient = dockerClientService.connect();
        try {
            InspectImageResponse inspectImageResponse = dockerClient.inspectImageCmd(id).exec();
            return ResultUtil.success(JSONObject.toJSON(inspectImageResponse));
        } catch (NotFoundException e) {
            return ResultUtil.error(ResultEnum.NOT_VALID_PARAM.getCode(), "找不到对应的镜像");
        }
    }

    @GetMapping("/all")
    public Result all() {
        DockerClient dockerClient = dockerClientService.connect();
        List<Image> imageList = dockerClient.listImagesCmd().withShowAll(true).exec();
        return ResultUtil.success(imageList);
    }
}
